/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;

import models.ResponseDTO;

/**
 *
 * @author User
 */
public class ResponseServiceImpl implements IEntity{
	
	Statement stmt=null;
    ResultSet rst=null;
    private ResponseDTO response;
    private List<ResponseDTO> responseList;

    public ResponseDTO GetById(int id) {
        try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from response where idResponse=" + id); 
            response.setidResponse(id);
            response.setIdQuestion(rst.getInt("idQuestion"));
            response.setIdUser(rst.getInt("idUser"));
            response.setResponseDesc(rst.getString("ResponseDesc"));
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(ResponseServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return response;
    }

    public List<ResponseDTO> GetAll() {
        try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from response ");
            while (rst.next()) {
                response.setidResponse(rst.getInt("idResponse"));
                response.setIdQuestion(rst.getInt("idQuestion"));
                response.setIdUser(rst.getInt("idUser"));
                response.setResponseDesc(rst.getString("ResponseDesc"));
                responseList.add(response);
            }
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(ResponseServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return responseList;
    }

    public void Add(Object obj){
    	
        ResponseDTO objCast=(ResponseDTO)obj;
        try {
			
        	stmt =  Configuration.GetStatement();
        	stmt.execute("INSERT INTO dbsurvey.response (`idResponse`, `idQuestion`, `idUser`, `ResponseDesc`)  VALUES ("+objCast.getIdResponse()+","+objCast.getIdQuestion()+","+objCast.getIdUser()+","+objCast.getResponseDesc()+")");
			Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    public void Update(Object obj){
    	
       ResponseDTO objCast=(ResponseDTO)obj;
       try {
		
    	   stmt = Configuration.GetStatement();
    	   stmt.execute("update dbsurvey.response set  idQuestion="+objCast.getIdQuestion()+"and idUser="+objCast.getIdUser()+"ResponseDesc="+objCast.getResponseDesc()+" where idResponse="+objCast.getIdResponse());
    	   Configuration.CloseConnection();
       
   	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    }

    public void Remove(int id){
    	
        try {
			
        	stmt = Configuration.GetStatement();
        	stmt.execute("delete from response where idResponse =" + id);
        	Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }


}
